package com.stone.message.adapter.infra.publisher;

import com.stone.message.adapter.domain.event.ReceiveMessageEvent;

/**
 * 消息接收器 具体接收消息操作由用户实现
 * 在业务代码中可以直接继承该类，然后在需要向容器发送消息的时候直接调用onReceivedMessage方法即可
 * 不用再自己去注入MessagePublisher，然后构建消息然后再发送了。也可以直接注入该类进行使用
 *
 * @author Mr_wenpan@163.com 2021/6/28 1:55 下午
 */
public class MessageReceivePublisher<T> {
    /**
     * 消息发送器
     */
    private final MessagePublisher messagePublisher;

    public MessageReceivePublisher(MessagePublisher messagePublisher) {
        this.messagePublisher = messagePublisher;
    }

    /**
     * 当接收到消息时，需要调用此方法抛出接收事件通知消费
     *
     * @param queue 消息队列
     * @param t     消息实体
     */
    protected void onReceivedMessage(String queue, T t) {
        ReceiveMessageEvent<T> receiveMessageEvent = new ReceiveMessageEvent<>(this);
        receiveMessageEvent.setContent(t);
        receiveMessageEvent.setQueue(queue);
        messagePublisher.publish(receiveMessageEvent);
    }
}